/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-08
*/
package com.rzico.market.entity;

import com.alipay.api.domain.OrderItem;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   豆讯话费订单管理数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "mk_phone_order")
public class PhoneOrder{
    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "订单编号" )
    protected String sn;

    @ApiModelProperty(value = "买家留言" )
    protected String memo;

    @ApiModelProperty(value = "充值结果" )
    protected Boolean result;

    @ApiModelProperty(value = "订单状态(0.未确认,1.已确认,2.已完成,3.已取消)" )
    protected Integer orderStatus;

    @ApiModelProperty(value = "支付状态(0.未支付,1.已支付,2.待退款,3.已退款)" )
    protected Integer paymentStatus;

    @ApiModelProperty(value = "活动优惠" )
    protected java.math.BigDecimal discount;

    @ApiModelProperty(value = "满减" )
    protected java.math.BigDecimal fullReduce;

    @ApiModelProperty(value = "商品合计金额" )
    protected java.math.BigDecimal subPrice;

    @ApiModelProperty(value = "商品合计数量" )
    protected java.math.BigDecimal subQuantity;

    @ApiModelProperty(value = "购买会员" )
    protected Long memberId;

    @JsonIgnore
    @ApiModelProperty(value = "是否删除" ,hidden = true )
    protected Boolean deleted;

    @ApiModelProperty(value = "订货方式(0.线上订单，1.人工报单，2.门店销售)" )
    protected Integer orderMethod;

    @ApiModelProperty(value = "实际销售日期" )
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "是否首单" )
    protected Boolean isFirst;

    @ApiModelProperty(value = "应付金额" )
    protected java.math.BigDecimal amountPayable;

    @ApiModelProperty(value = "已付金额" )
    protected java.math.BigDecimal amountPaid;

    @ApiModelProperty(value = "归属企业" )
    protected Long enterpriseId;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    @ApiModelProperty(value = "配送单明细")
    List<PhoneOrderItem> itemList = Collections.emptyList();


    @JsonIgnore
    public String getTitle() {
        for (PhoneOrderItem phoneOrderItem:getItemList()) {
            return phoneOrderItem.getName();
        }
        return null;
    }

    //订单下所有商品的总金额
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubPrice() {

        BigDecimal totalSubPrice = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (PhoneOrderItem phoneOrderItem : itemList){
                BigDecimal subPriceTotal = phoneOrderItem.getPrice().multiply(phoneOrderItem.getQuantity()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
                totalSubPrice =  totalSubPrice.add(subPriceTotal);
            }
        }
        return totalSubPrice.setScale(2,BigDecimal.ROUND_HALF_DOWN);

    }

    //订单商品总金额-满减-折扣
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateAmountPayable() {

        BigDecimal totalAmountPayable = calculateSubPrice();

        BigDecimal offsetAmount = null == this.fullReduce?BigDecimal.ZERO : this.fullReduce;
        BigDecimal discount = null == this.discount?BigDecimal.ZERO : this.discount;
        // +加上运费 + 楼层费 - 抹零 - 优惠券抵扣 - 金币抵扣 - 提货券抵扣 - 营销折扣 - 纸质券抵扣金额 - 全场活动折扣
        totalAmountPayable = totalAmountPayable.subtract(fullReduce).subtract(discount);
        return totalAmountPayable.setScale(2,BigDecimal.ROUND_HALF_DOWN);

    }

    //订单下所有商品的数量
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public BigDecimal calculateSubQuantity(){
        BigDecimal totalQuantity = new BigDecimal(0);
        if (null != itemList && itemList.size() > 0){
            for (PhoneOrderItem phoneOrderItem : itemList){
                totalQuantity = totalQuantity.add(phoneOrderItem.getQuantity());
            }
        }
        return totalQuantity.setScale(2,BigDecimal.ROUND_HALF_DOWN);
    }

}
